import logging
from loguru import logger
<<<<<<< HEAD
from op_drf.dj_utils import create_by_serializer
from op_drf.serializers import CustomModelSerializer, BaseSerializer
from rest_framework import serializers
from apps.Order.models import (
Order
=======

from apps.Order.models import Order
from op_drf.dj_utils import create_by_serializer, flow_code_manage, check_serial_num
from op_drf.serializers import CustomModelSerializer, BaseSerializer
from rest_framework import serializers
from apps.prescription.models import (
Prescription,
Drug
>>>>>>> dev
)

import datetime

<<<<<<< HEAD
# 新建订单信息
class WriteOrderSerializer(CustomModelSerializer):

    # 默认自提
    pick_type = serializers.CharField(default="0")
    staruser = serializers.CharField(default=53)

    def validate(self, attrs: dict):
        return super().validate(attrs)

    def create(self, validated_data):
        # 增加数据的验证
        logger.debug("请求携带的数据： %s" % validated_data)
        validated_data["ordernum"]="自动生成"
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)

=======
# 新建处方信息
class WriteOrderSerializer(CustomModelSerializer):
    order_date = serializers.DateTimeField(format="%Y-%m-%d",default=datetime.datetime.now())
    # OrderNum = serializers.CharField(required=True)
    # # 默认专业方
    # OrderType = serializers.CharField(default="0")

    def create(self, validated_data):
        # 增加数据的验证
        serial_num = flow_code_manage(value="order_num")
        check_serial_num(serial_num=serial_num, model=self.Meta.model, check_field_name="ordernum")
        validated_data["ordernum"] = serial_num
        logger.debug("请求携带的数据： %s" % validated_data)
        return super().create(validated_data)

>>>>>>> dev
    class Meta:
        model = Order
        fields = "__all__"


<<<<<<< HEAD
class ReadOrderSerializer(CustomModelSerializer):
=======
'''
读取订单列表
'''
class ReadOrderSerializer(BaseSerializer):
    order_date = serializers.DateTimeField(format="%Y-%m-%d")
    Prescription_Num = serializers.CharField(source="prescription.OrderNum",read_only=True)
    OrderStatus_name = serializers.SerializerMethodField(read_only=True)
    rad_name = serializers.SerializerMethodField(read_only=True)

    def get_OrderStatus_name(self, obj):
        if obj.OrderStatus:
            if obj.OrderStatus=="1":
                return "已摆药"
            else:
                return "未摆药"

    def get_rad_name(self, obj):
        if obj.rad:
            return obj.rad.name if obj.rad else ""
>>>>>>> dev

    class Meta:
        model = Order
        fields = "__all__"

